package com.mxchip.livestarmobile.mobile.ui.call

import android.app.Application
import androidx.lifecycle.*
import com.mxchip.livestarmobile.mobile.Appli
import com.mxchip.livestarmobile.mobile.db.AppDatabase
import com.mxchip.livestarmobile.mobile.db.entity.CallLog
import com.mxchip.livestarmobile.mobile.state.AppConstant
import com.orhanobut.hawk.Hawk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.*

class RedViewModel(application: Application) : AndroidViewModel(application) {

    private val appDatabase: AppDatabase = (application as Appli).database

    private val userId: String = Hawk.get(AppConstant.SP_LOGIN_USER_ID)

    fun insertCallLog(number: String, type: String) {

        val callLog = CallLog(userId = userId)
        callLog.number = number
        callLog.type = type
        callLog.startTime = Date()
        callLog.isCaller = true

        GlobalScope.launch(Dispatchers.Main) {
            appDatabase.calllogDao().insertAll(callLog)
        }
    }


}
